home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / PERL.SPK / Perl5001 / Manual / perltrap_h < prev    next >
Text File  |  1995-04-18  |  13KB  |  487 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLTRAP </TITLE>
  5. <h2>NAME</h2>
  6. perltrap - Perl traps for the unwary
  7. <p><h2>DESCRIPTION</h2>
  8. The biggest trap of all is forgetting to use the 
  9. <A HREF="perlrun.html#perlrun_362">-w</A>
  10.  switch;
  11. see 
  12. <A HREF="perlrun.html">
  13. the perlrun manpage</A>
  14. .  Making your entire program runnable under
  15. <p><pre>
  16.         use strict;
  17. </pre>
  18. can help make your program more bullet-proof, but sometimes
  19. it's too annoying for quick throw-away programs.
  20. <p><h3>Awk Traps</h3>
  21. Accustomed <B>awk</B> users should take special note of the following:
  22. <p>
  23. <dl>
  24. <dt><B>*</B>
  25. <dd>
  26. The English module, loaded via
  27. <p></dd>
  28. <pre>
  29.         use English;
  30. </pre>
  31. allows you to refer to special variables (like $RS) as 
  32. though they were in <B>awk</B>; see 
  33. <A HREF="perlvar.html">
  34. the perlvar manpage</A>
  35.  for details.
  36. <p><dt><B>*</B>
  37. <dd>
  38. Semicolons are required after all simple statements in Perl (except
  39. at the end of a block).  Newline is not a statement delimiter.
  40. <p></dd>
  41. <dt><B>*</B>
  42. <dd>
  43. Curly brackets are required on <B>if</B>s and <B>while</B>s.
  44. <p></dd>
  45. <dt><B>*</B>
  46. <dd>
  47. Variables begin with "$" or "@" in Perl.
  48. <p></dd>
  49. <dt><B>*</B>
  50. <dd>
  51. Arrays index from 0.  Likewise string positions in substr() and
  52. index().
  53. <p></dd>
  54. <dt><B>*</B>
  55. <dd>
  56. You have to decide whether your array has numeric or string indices.
  57. <p></dd>
  58. <dt><B>*</B>
  59. <dd>
  60. Associative array values do not spring into existence upon mere
  61. reference.
  62. <p></dd>
  63. <dt><B>*</B>
  64. <dd>
  65. You have to decide whether you want to use string or numeric
  66. comparisons.
  67. <p></dd>
  68. <dt><B>*</B>
  69. <dd>
  70. Reading an input line does not split it for you.  You get to split it
  71. yourself to an array.  And split() operator has different
  72. arguments.
  73. <p></dd>
  74. <dt><B>*</B>
  75. <dd>
  76. The current input line is normally in $_, not $0.  It generally does
  77. not have the newline stripped.  ($0 is the name of the program
  78. executed.)  See 
  79. <A HREF="perlvar.html">
  80. the perlvar manpage</A>
  81. .
  82. <p></dd>
  83. <dt><B>*</B>
  84. <dd>
  85. $<<I>digit</I>> does not refer to fields--it refers to substrings matched by
  86. the last match pattern.
  87. <p></dd>
  88. <dt><B>*</B>
  89. <dd>
  90. The print() statement does not add field and record separators unless
  91. you set 
  92. <A HREF="perlvar.html#perlvar_397">$,</A>
  93.  and 
  94. <A HREF="perlvar.html#perlvar_386">$.</A>
  95. .  You can set $OFS and $ORS if you're using
  96. the English module.
  97. <p></dd>
  98. <dt><B>*</B>
  99. <dd>
  100. You must open your files before you print to them.
  101. <p></dd>
  102. <dt><B>*</B>
  103. <dd>
  104. The range operator is "..", not comma.  The comma operator works as in
  105. C.
  106. <p></dd>
  107. <dt><B>*</B>
  108. <dd>
  109. The match operator is "=~", not "~".  ("~" is the one's complement
  110. operator, as in C.)
  111. <p></dd>
  112. <dt><B>*</B>
  113. <dd>
  114. The exponentiation operator is "**", not "^".  "^" is the XOR
  115. operator, as in C.  (You know, one could get the feeling that <B>awk</B> is
  116. basically incompatible with C.)
  117. <p></dd>
  118. <dt><B>*</B>
  119. <dd>
  120. The concatenation operator is ".", not the null string.  (Using the
  121. null string would render <B>/pat/ /pat/</B> unparsable, since the third slash
  122. would be interpreted as a division operator--the tokener is in fact
  123. slightly context sensitive for operators like "/", "?", and ">".
  124. And in fact, "." itself can be the beginning of a number.)
  125. <p></dd>
  126. <dt><B>*</B>
  127. <dd>
  128. The 
  129. <A HREF="perlfunc.html#perlfunc_179">next</A>
  130. <A HREF="perlfunc.html#perlfunc_104">exit</A>
  131. , and <B>continue</B> keywords work differently.
  132. <p></dd>
  133. <dt><B>*</B>
  134. <dd>
  135. The following variables work differently:
  136. <p></dd>
  137.  
  138. <listing>
  139.       Awk    Perl 
  140.       ARGC    $#ARGV or scalar @ARGV 
  141.       ARGV[0]    $0 
  142.       FILENAME    $ARGV 
  143.       FNR    $. - something 
  144.       FS    (whatever you like) 
  145.       NF    $#Fld, or some such 
  146.       NR    $. 
  147.       OFMT    $# 
  148.       OFS    $, 
  149.       ORS    $\ 
  150.       RLENGTH    length($&) 
  151.       RS    $/ 
  152.       RSTART    length($`) 
  153.       SUBSEP    $; 
  154. </listing>
  155. <dt><B>*</B>
  156. <dd>
  157. You cannot set $RS to a pattern, only a string.
  158. <p></dd>
  159. <dt><B>*</B>
  160. <dd>
  161. When in doubt, run the <B>awk</B> construct through <B>a2p</B> and see what it
  162. gives you.
  163. <p></dd>
  164.  
  165. </dl>
  166.  
  167. <h3>C Traps</h3>
  168. Cerebral C programmers should take note of the following:
  169. <p>
  170. <dl>
  171. <dt><B>*</B>
  172. <dd>
  173. Curly brackets are required on <B>if</B>'s and <B>while</B>'s.
  174. <p></dd>
  175. <dt><B>*</B>
  176. <dd>
  177. You must use <B>elsif</B> rather than <B>else if</B>.
  178. <p></dd>
  179. <dt><B>*</B>
  180. <dd>
  181. The <B>break</B> and <B>continue</B> keywords from C become in 
  182. Perl 
  183. <A HREF="perlfunc.html#perlfunc_161">last</A>
  184.  and 
  185. <A HREF="perlfunc.html#perlfunc_179">next</A>
  186. , respectively.
  187. Unlike in C, these do <I>NOT</I> work within a <B>do { } while</B> construct.
  188. <p></dd>
  189. <dt><B>*</B>
  190. <dd>
  191. There's no switch statement.  (But it's easy to build one on the fly.)
  192. <p></dd>
  193. <dt><B>*</B>
  194. <dd>
  195. Variables begin with "$" or "@" in Perl.
  196. <p></dd>
  197. <dt><B>*</B>
  198. <dd>
  199. printf() does not implement the "*" format for interpolating
  200. field widths, but it's trivial to use interpolation of double-quoted
  201. strings to achieve the same effect.
  202. <p></dd>
  203. <dt><B>*</B>
  204. <dd>
  205. Comments begin with "#", not "/*".
  206. <p></dd>
  207. <dt><B>*</B>
  208. <dd>
  209. You can't take the address of anything, although a similar operator
  210. in Perl 5 is the backslash, which creates a reference.
  211. <p></dd>
  212. <dt><B>*</B>
  213. <dd>
  214. <B>ARGV</B> must be capitalized.
  215. <p></dd>
  216. <dt><B>*</B>
  217. <dd>
  218. System calls such as link(), unlink(), rename(), etc. return nonzero for
  219. success, not 0.
  220. <p></dd>
  221. <dt><B>*</B>
  222. <dd>
  223. Signal handlers deal with signal names, not numbers.  Use <B>kill -l</B>
  224. to find their names on your system.
  225. <p></dd>
  226.  
  227. </dl>
  228.  
  229. <h3>Sed Traps</h3>
  230. Seasoned <B>sed</B> programmers should take note of the following:
  231. <p>
  232. <dl>
  233. <dt><B>*</B>
  234. <dd>
  235. Backreferences in substitutions use "$" rather than "\".
  236. <p></dd>
  237. <dt><B>*</B>
  238. <dd>
  239. The pattern matching metacharacters "(", ")", and "|" do not have backslashes
  240. in front.
  241. <p></dd>
  242. <dt><B>*</B>
  243. <dd>
  244. The range operator is <B>...</B>, rather than comma.
  245. <p></dd>
  246.  
  247. </dl>
  248.  
  249. <h3>Shell Traps</h3>
  250. Sharp shell programmers should take note of the following:
  251. <p>
  252. <dl>
  253. <dt><B>*</B>
  254. <dd>
  255. The backtick operator does variable interpretation without regard to
  256. the presence of single quotes in the command.
  257. <p></dd>
  258. <dt><B>*</B>
  259. <dd>
  260. The backtick operator does no translation of the return value, unlike <B>csh</B>.
  261. <p></dd>
  262. <dt><B>*</B>
  263. <dd>
  264. Shells (especially <B>csh</B>) do several levels of substitution on each
  265. command line.  Perl does substitution only in certain constructs
  266. such as double quotes, backticks, angle brackets, and search patterns.
  267. <p></dd>
  268. <dt><B>*</B>
  269. <dd>
  270. Shells interpret scripts a little bit at a time.  Perl compiles the
  271. entire program before executing it (except for <B>BEGIN</B> blocks, which
  272. execute at compile time).
  273. <p></dd>
  274. <dt><B>*</B>
  275. <dd>
  276. The arguments are available via @ARGV, not $1, $2, etc.
  277. <p></dd>
  278. <dt><B>*</B>
  279. <dd>
  280. The environment is not automatically made available as separate scalar
  281. variables.
  282. <p></dd>
  283.  
  284. </dl>
  285.  
  286. <h3>Perl Traps</h3>
  287. Practicing Perl Programmers should take note of the following:
  288. <p>
  289. <dl>
  290. <dt><B>*</B>
  291. <dd>
  292. Remember that many operations behave differently in a list
  293. context than they do in a scalar one.  See 
  294. <A HREF="perldata.html">
  295. the perldata manpage</A>
  296.  for details.
  297. <p></dd>
  298. <dt><B>*</B>
  299. <dd>
  300. Avoid barewords if you can, especially all lower-case ones.
  301. You can't tell just by looking at it whether a bareword is 
  302. a function or a string.  By using quotes on strings and 
  303. parens on function calls, you won't ever get them confused.
  304. <p></dd>
  305. <dt><B>*</B>
  306. <dd>
  307. You cannot discern from mere inspection which built-ins
  308. are unary operators (like chop() and chdir()) 
  309. and which are list operators (like print() and unlink()).
  310. (User-defined subroutines can <B>only</B> be list operators, never
  311. unary ones.)  See 
  312. <A HREF="perlop.html">
  313. the perlop manpage</A>
  314. .
  315. <p></dd>
  316. <dt><B>*</B>
  317. <dd>
  318. People have a hard type remembering that some functions
  319. default to $_, or @ARGV, or whatever, but that others which
  320. you might expect to do not.  
  321. <p></dd>
  322. <dt><B>* </B>
  323. <dd>
  324. Remember not to use "<B>=</B>" when you need "<B>=~</B>";
  325. these two constructs are quite different:
  326. <p></dd>
  327. <pre>
  328.         $x =  /foo/;
  329.         $x =~ /foo/;
  330. </pre>
  331. <dt><B>*</B>
  332. <dd>
  333. The <B>do {}</B> construct isn't a real loop that you can use 
  334. loop control on.
  335. <p></dd>
  336. <dt><B>*</B>
  337. <dd>
  338. Use my() for local variables whenever you can get away with 
  339. it (but see 
  340. <A HREF="perlform.html">
  341. the perlform manpage</A>
  342.  for where you can't).  
  343. Using local() actually gives a local value to a global 
  344. variable, which leaves you open to unforeseen side-effects
  345. of dynamic scoping.
  346. <p></dd>
  347.  
  348. </dl>
  349.  
  350. <h3>Perl4 Traps</h3>
  351. Penitent Perl 4 Programmers should take note of the following
  352. incompatible changes that occurred between release 4 and release 5:
  353. <p>
  354. <dl>
  355. <dt><B>*</B>
  356. <dd>
  357. <B>@</B> now always interpolates an array in double-quotish strings.  Some programs
  358. may now need to use backslash to protect any <B>@</B> that shouldn't interpolate.
  359. <p></dd>
  360. <dt><B>* Barewords that used to look like strings to Perl will now look like subroutine calls if a subroutine by that name is defined before the compiler sees them. For example:</B>
  361. <pre>
  362.         sub SeeYa { die "Hasta la vista, baby!" }
  363.         $SIG{QUIT} = SeeYa;
  364. </pre>
  365. <dd>
  366. In Perl 4, that set the signal handler; in Perl 5, it actually calls the
  367. function!  You may use the 
  368. <A HREF="perlrun.html#perlrun_362">-w</A>
  369.  switch to find such places.
  370. <p></dd>
  371. <dt><B>*</B>
  372. <dd>
  373. Symbols starting with <B>_</B> are no longer forced into package <B>main</B>, except
  374. for $_ itself (and @_, etc.).
  375. <p></dd>
  376. <dt><B>*</B>
  377. <dd>
  378. <B>s'$lhs'$rhs'</B> now does no interpolation on either side.  It used to
  379. interpolate <B>$lhs</B> but not <B>$rhs</B>.
  380. <p></dd>
  381. <dt><B>*</B>
  382. <dd>
  383. The second and third arguments of splice() are now evaluated in scalar
  384. context (as the book says) rather than list context.
  385. <p></dd>
  386. <dt><B>*</B>
  387. <dd>
  388. These are now semantic errors because of precedence:
  389. <p></dd>
  390. <pre>
  391.         shift @list + 20;       
  392.         $n = keys %map + 20; 
  393. </pre>
  394. Because if that were to work, then this couldn't:
  395. <p><pre>
  396.         sleep $dormancy + 20;
  397. </pre>
  398. <dt><B>*</B>
  399. <dd>
  400. <B>open FOO || die</B> is now incorrect.  You need parens around the filehandle.
  401. While temporarily supported, using such a construct will 
  402. generate a non-fatal (but non-suppressible) warning.
  403. <p></dd>
  404. <dt><B>*</B>
  405. <dd>
  406. The elements of argument lists for formats are now evaluated in list
  407. context.  This means you can interpolate list values now.
  408. <p></dd>
  409. <dt><B>*</B>
  410. <dd>
  411. You can't do a 
  412. <A HREF="perlfunc.html#perlfunc_151">goto</A>
  413.  into a block that is optimized away.  Darn.
  414. <p></dd>
  415. <dt><B>*</B>
  416. <dd>
  417. It is no longer syntactically legal to use whitespace as the name
  418. of a variable, or as a delimiter for any kind of quote construct.
  419. Double darn.
  420. <p></dd>
  421. <dt><B>*</B>
  422. <dd>
  423. The caller() function now returns a false value in a scalar context if there
  424. is no caller.  This lets library files determine if they're being required.
  425. <p></dd>
  426. <dt><B>*</B>
  427. <dd>
  428. <B>m//g</B> now attaches its state to the searched string rather than the
  429. regular expression.
  430. <p></dd>
  431. <dt><B>*</B>
  432. <dd>
  433.  
  434. <A HREF="perlfunc.html#perlfunc_208">reverse</A>
  435.  is no longer allowed as the name of a sort subroutine.
  436. <p></dd>
  437. <dt><B>*</B>
  438. <dd>
  439. <B>taintperl</B> is no longer a separate executable.  There is now a 
  440. <A HREF="perlrun.html#perlrun_358">-T</A>
  441.  
  442. switch to turn on tainting when it isn't turned on automatically.
  443. <p></dd>
  444. <dt><B>*</B>
  445. <dd>
  446. Double-quoted strings may no longer end with an unescaped <B>$</B> or <B>@</B>.
  447. <p></dd>
  448. <dt><B>*</B>
  449. <dd>
  450. The archaic <B>while/if</B> BLOCK BLOCK syntax is no longer supported.
  451. <p></dd>
  452. <dt><B>*</B>
  453. <dd>
  454. Negative array subscripts now count from the end of the array.
  455. <p></dd>
  456. <dt><B>*</B>
  457. <dd>
  458. The comma operator in a scalar context is now guaranteed to give a
  459. scalar context to its arguments.
  460. <p></dd>
  461. <dt><B>*</B>
  462. <dd>
  463. The <B>**</B> operator now binds more tightly than unary minus.  
  464. It was documented to work this way before, but didn't.
  465. <p></dd>
  466. <dt><B>*</B>
  467. <dd>
  468. Setting <B>$#array</B> lower now discards array elements.
  469. <p></dd>
  470. <dt><B>*</B>
  471. <dd>
  472. delete() is not guaranteed to return the old value for tie()d arrays,
  473. since this capability may be onerous for some modules to implement.
  474. <p></dd>
  475. <dt><B>*</B>
  476. <dd>
  477. Some error messages will be different.
  478. <p></dd>
  479. <dt><B>*</B>
  480. <dd>
  481. Some bugs may have been inadvertently removed.
  482. <p></dd>
  483.  
  484. </dl>
  485.  
  486.